package controller;

import entity.Competitions;
import service.CompetitionsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/competitions")
public class CompetitionsController {

    @Autowired
    private CompetitionsService competitionsService;

    @GetMapping("/{id}")
    public Competitions getById(@PathVariable Integer id) {
        return competitionsService.getById(id);
    }

    @GetMapping("/all")
    public List<Competitions> getAll() {
        return competitionsService.getAll();
    }

    @PostMapping
    public Map<String, Object> create(@RequestBody Competitions obj) {
        int result = competitionsService.create(obj);
        if (result > 0) {
            return Map.of("code", 200, "message", "添加成功");
        } else {
            return Map.of("code", 500, "message", "添加失败");
        }
    }

    @PutMapping
    public Map<String, Object> update(@RequestBody Competitions obj) {
        int result = competitionsService.update(obj);
        if (result > 0) {
            return Map.of("code", 200, "message", "更新成功");
        } else {
            return Map.of("code", 500, "message", "更新失败");
        }
    }
    @GetMapping("/student/{studentId}")
    public List<Competitions> getByStudentId(@PathVariable Integer studentId) {
        return competitionsService.getByStudentId(studentId);
    }

    @DeleteMapping("/{id}")
    public Map<String, Object> delete(@PathVariable Integer id) {
        int result = competitionsService.delete(id);
        if (result > 0) {
            return Map.of("code", 200, "message", "删除成功");
        } else {
            return Map.of("code", 500, "message", "删除失败");
        }
    }
}
